package com.example.mapinformationandroid.UI;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.example.mapinformationandroid.ConstantData.Define;
import com.example.mapinformationandroid.R;
import com.example.mapinformationandroid.UI.Fragment.LoginFragment;
import com.example.mapinformationandroid.UI.Fragment.SignUpFragment;

import com.example.mapinformationandroid.Util.Px2DpUtil;
import com.example.mapinformationandroid.Util.ScreenUtil;

public class Login extends AppCompatActivity {

    private SharedPreferences sharedPreferences;
    private boolean isLogin = true;
    private Fragment[] mFragments;
    private Fragment mLastFragment;
    private RelativeLayout mRl;
    private Button mBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initData();
        initView();

    }

    private void initView() {
        mRl = (RelativeLayout) findViewById(R.id.id_rl);
        mBtn = (Button) findViewById(R.id.id_btn);
        mFragments = new Fragment[]{new LoginFragment(), new SignUpFragment()};
        switchLogin();
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isLogin();
            }
        });
    }

    private void initData() {
        sharedPreferences = getSharedPreferences("user", Activity.MODE_PRIVATE);
        String token = sharedPreferences.getString(Define.UserDefine.USER_TOKEN, "");
        Log.d("Login", token);
        if (!TextUtils.isEmpty(token)) {
            startActivity(new Intent(this, Home.class));
            finish();
        }
    }

    private void btnTranslateLeft() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mBtn, "translationX",-ScreenUtil.getScreenWidth(this)-Px2DpUtil.dp2px(this,120),-ScreenUtil.getScreenWidth(this)+Px2DpUtil.dp2px(this,120));
        objectAnimator.setDuration(getResources().getInteger(R.integer.anim_short));
        objectAnimator.setInterpolator(new BounceInterpolator());
        objectAnimator.start();
    }


    private void btnTranslateRight() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mBtn, "translationX",  Px2DpUtil.dp2px(this, 120), -Px2DpUtil.dp2px(this, 20));
        objectAnimator.setDuration(getResources().getInteger(R.integer.anim_short));
        objectAnimator.setInterpolator(new BounceInterpolator());
        objectAnimator.start();
    }

    private void animColor(int colorTo) {
        ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), R.color.colorAccent, colorTo);
        colorAnimation.setDuration(getResources().getInteger(R.integer.anim_short));
        colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                int color = (int) animator.getAnimatedValue();
                mRl.setBackgroundColor(color);
            }
        });
        colorAnimation.start();
    }


    public void switchLogin() {
        isLogin = true;
        switchFragment(mFragments[0], R.anim.rotate_fg_enter_left, R.anim.rotate_fg_exit_left);
//        animColor(ContextCompat.getColor(this, R.color.background));
        btnTranslateRight();
        mBtn.setText("去注册");
    }

    public void switchSignUp() {
        isLogin = false;
        switchFragment(mFragments[1], R.anim.rotate_fg_enter_right, R.anim.rotate_fg_exit_right);
//        animColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
        btnTranslateLeft();
        mBtn.setText("去登录");
    }

    private void isLogin() {
        if (isLogin) {
            switchSignUp();
        } else {
            switchLogin();
        }
    }


    /**
     * Fragment切换
     *
     * @param fragment
     */
    public void switchFragment(Fragment fragment, int enter, int exit) {
        try {
            if (fragment == null) {
                fragment = mFragments[0];
            }
            if (fragment.equals(mLastFragment)) {
                return;
            }

            FragmentManager mFragmentManager = getSupportFragmentManager();
            FragmentTransaction mTransaction = mFragmentManager.beginTransaction();
            mTransaction.setCustomAnimations(enter, exit);

            if (mLastFragment != null) {
                mTransaction.hide(mLastFragment);
            }
            if (!fragment.isAdded()) {
                mTransaction.add(R.id.id_fl_login_in, fragment);
            } else {
                mTransaction.show(fragment);
            }
            mTransaction.commitAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mLastFragment = fragment;
        }
    }
}
